package cn.sytton.taffecloud.service.file.controller;

import cn.sytton.taffecloud.common.base.doc.*;
import cn.sytton.taffecloud.service.file.service.IFileService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 文件服务控制层
 *
 * @author skyrock
 */
@Api(tags = "文件")
@RestController
@RequestMapping
public class FileController {

    @Resource
    private IFileService fileService;

    @ApiOperation("下载文件")
    @ApiImplicitParam(name = "fileId", value = "文件ID", paramType = ParamType.path)
    @GetMapping("/download/{fileId}")
    public void download(@PathVariable String fileId) {
        fileService.download(fileId);
    }

    @ApiOperation("下载文件组,返回zip压缩包")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "groupId", value = "文件组ID", paramType = ParamType.path),
        @ApiImplicitParam(name = "name", value = "下载文件命名", paramType = ParamType.query, required = false)
    })
    @GetMapping("/group/download/{groupId}")
    public void downloadGroup(@PathVariable String groupId, @RequestParam(value = "name", required = false) String name) {
        fileService.downloadGroup(groupId, name);
    }

    @ApiOperation("获取文件访问路径")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileId", value = "文件ID", paramType = ParamType.path),
            @ApiImplicitParam(name = "public", value = "是否可永久访问", paramType = ParamType.query, required = false, dataTypeClass = Boolean.class)
    })
    @GetMapping("/{fileId}")
    public String getUrl(@PathVariable String fileId, @RequestParam(value = "public", required = false) Boolean isPublic) {
        if (isPublic == null || !isPublic) {
            return fileService.getUrl(fileId);
        } else {
            return fileService.getPublicUrl(fileId);
        }
    }
}
